﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.DAL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class ES_ScholarRequest : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        ITrainingScholarshipTypeBLL SchTypeBLL = new TrainingScholarshipTypeBLL();
        ITrainingScholarshipFundBLL SchFundBLL = new TrainingScholarshipFundBLL();
        InitialData_BLL iniDataCur = new InitialData_BLL();
        TR_SchlarshipDtlsBLL SchDtlsBLL = new TR_SchlarshipDtlsBLL();
        IMasEducationMajorBLL majorBLL = new MasEducationMajorBLL();
        private IMasEducationInstituteBLL insBLL = new MasEducationInstituteBLL();
        IEmployeeBLL EmpBLL = new EmployeeBLL();
        ConvertNullable cn = new ConvertNullable();
        TrainingScholarshipsRequestsBLL TrSchReqBLL = new TrainingScholarshipsRequestsBLL();
        TrainingScholarshipBLL SchBLL = new TrainingScholarshipBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
                //CBTrainingCur();
                LoadScholarGroup();
                LoadEduMajors();
                LoadEduInstitutes();
                LoadEmployeeList();
                LoadScholar();
            }
        }

        private void LoadDataGrid()
        {

            DataSet ListDataCommand = TrSchReqBLL.GetDataSchReq(ssMng.EmpSeq,"");
            DataTable table = ListDataCommand.Tables[0];
            RadGrid2.DataSource = ListDataCommand.Tables[0];
            RadGrid2.DataBind();

        }
        private void LoadScholarGroup()
        {
            DataSet ListDataCommand = SchDtlsBLL.GetDataSchGroup("");
            DataTable table = ListDataCommand.Tables[0];
            cbScholarGroup.DataSource = ListDataCommand.Tables[0];
            cbScholarGroup.DataBind();           
        }
        protected void LoadEmployeeList()
        {
            string strSeq = "";
            Nullable<int> Seq = cn.ConvertNullInt(strSeq);
            DataSet ListSalaryCommand = EmpBLL.LoadEmployees(Seq, "1", true, "");
            DataTable table = ListSalaryCommand.Tables[0];
            cbEmployeeList.DataSource = ListSalaryCommand.Tables[0];
            cbEmployeeList.DataBind();
        }
        private void LoadScholar()
        {
            DataSet ListDataCommand = SchBLL.GetData("");
            DataTable table = ListDataCommand.Tables[0];
            cbScholar.DataSource = ListDataCommand.Tables[0];
            cbScholar.DataBind();
        }
        private void LoadEduInstitutes()
        {
            DataSet ListDataCommand = TrSchReqBLL.GetDatacbEduInstitutes("");
            DataTable table = ListDataCommand.Tables[0]; 
            cbEduInstitutes.DataSource = ListDataCommand.Tables[0];
            cbEduInstitutes.DataBind();
            //IList<MasEducationInstituteInfo> Info = insBLL.ListEducationInstitutes();
            //cbEduInstitutes.DataSource = Info;
            //cbEduInstitutes.DataBind();

        }

        private void LoadEduMajors()
        {
            IList<MasEducationMajorInfo> Info = majorBLL.ListEducationMajors();
            cbEduMajors.DataSource = Info;
            cbEduMajors.DataBind();
        }

        protected void RadGrid2_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI("AddNew");
            }
            else if (e.CommandName == "DataEdit")
            {
                string tran_seq = RadGrid2.Items[e.Item.ItemIndex]["tran_seq"].Text;
                txtscholarSeq.Text = tran_seq;
                DataSet ListDataCommand = TrSchReqBLL.GetDataSchReq(ssMng.EmpSeq, tran_seq);
                DataTable table = ListDataCommand.Tables[0];
                if (table.Rows.Count > 0)
                {
                    cbScholar.SelectedValue = table.Rows[0]["scholar_seq"].ToString();
                    cbScholarGroup.SelectedValue = table.Rows[0]["scholar_group_seq"].ToString();
                    cbEduMajors.SelectedValue = table.Rows[0]["edu_major_seq"].ToString();
                    txtYears.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["duration_year"]);
                    txtMonth.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["duration_month"]);
                    //cbEduInstitutes.SelectedValue = table.Rows[0]["scholar_fund_seq"].ToString();
                    //txtTeacherName.Text = table.Rows[0]["scholar_fund_seq"].ToString();
                    //txtDateSignStart.SetValue(string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_start"]));
                    //txtDateResponse.Text = table.Rows[0]["scholar_fund_seq"].ToString();
                    //txtResponseNo.Text = table.Rows[0]["scholar_fund_seq"].ToString();
                    //txtAmountRequest.Text = string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_start"]);
                    //txtExpectance.Text = table.Rows[0]["scholar_fund_seq"].ToString();
                    //cbEmployeeList.SelectedValue = table.Rows[0]["scholar_fund_seq"].ToString();

                    //if (table.Rows[0]["scholar_fund_seq2"].ToString().Length > 0)
                    //{ ddlScholarFund2.SelectedValue = table.Rows[0]["scholar_fund_seq2"].ToString(); }
                    //cbEduDegrees.SelectedValue = table.Rows[0]["edu_degree_seq"].ToString();
                    //ddlCountry.SelectedValue = table.Rows[0]["country_seq"].ToString();
                    ////txtEmpPreCon.Text = table.Rows[0][""].ToString();
                    ////txtBudget.Text = table.Rows[0]["amount"].ToString();
                    //string strStatus = table.Rows[0]["closed"].ToString();
                    //if (strStatus == "1")
                    //{ rbStatus.Checked = true; }
                    //else
                    //{ rbStatus.Checked = false; }
                    //txtDateSignStart.SetValue(string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_start"]));
                    //txtDateSignEnd.SetValue(string.Format("{0:dd/MM/yyyy}", table.Rows[0]["date_sign_end"]));
                    ////txtDateSignEnd
                    //txtYearEnd.Text = table.Rows[0]["edu_year_end"].ToString();
                    //txtYearStart.Text = table.Rows[0]["edu_year_start"].ToString();
                }
                UpdateUI("Update");
            }
            LoadDataGrid();
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            UpdateUI("Save");
            LoadDataGrid();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            TrSchReqBLL.InsertSchReq(ssMng.EmpSeqI,Convert.ToInt32(cbScholar.SelectedValue),Convert.ToInt32(cbEduInstitutes.SelectedValue)
                                    ,Convert.ToInt32(cbEduMajors.SelectedValue),txtTeacherName.Text, txtResponseNo.Text
                                    , Convert.ToDateTime(txtDateResponse.Text), txtDateSignStart.GetDateValue(), Convert.ToInt32(txtMonth.Text), Convert.ToInt32(txtYears.Text)
                                    , Convert.ToInt32(txtAmountRequest.Text), ssMng.EmpSeq);
            UpdateUI("Save");
            LoadDataGrid();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
        }
        protected void ClearControl()
        {
            //txtDateSignStart.SetValue("");
            //txtDateSignEnd.SetValue("");
            //txtYearEnd.Text = "";
            //txtYearStart.Text = "";
            //    rblCoutry.SelectedValue = "1";
            //    lblError.Text = "";
            //    txtScholaType.Text = "";
            //    txtScholaTypeSeq.Text = "";
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            //string strScholaType = cbScholaType.Text;
            //if (strScholaType.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณาเลือกแหล่งทุน";
            //}
            //string strScholarFund1 = ddlScholarFund1.Text;
            //if (strScholarFund1.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณาเลือกแหล่งเงิน";
            //}
            //string DateSignStart = txtDateSignStart.GetStringValue();
            //if (DateSignStart.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณากรอกปีการศึกษาที่เริ่มให้ทุน";
            //}
            //string DateSignEnd = txtDateSignEnd.GetStringValue();
            //if (DateSignEnd.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณากรอกปีการศึกษาที่สิ้นสุดการให้ทุน";
            //}
            //if (txtYearStart.Text.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณาเลือกวันที่ปิดรับสมัคร";
            //}
            //if (DateSignEnd.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณาเลือกวันที่ปิดรับสมัคร";
            //}
            return bOK;
        }
        //private void CBTrainingCur()
        //{
        //    cbTrainingCur.DataSource = iniDataCur.ListTrainingCurriculums("");
        //    cbTrainingCur.DataBind();
        //}
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    //btnUpdate.Visible = false;
                    //btnSave.Visible = true;

                    break;

                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
            }
        }
    }
}
